package com.cskaoyan.service.impl;

import com.cskaoyan.bean.goods.Bo.BaseParamBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketFootprint;
import com.cskaoyan.bean.po.MarketFootprintExample;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.usersadmin.FootprintData;
import com.cskaoyan.bean.wx.footprint.FootPrintOfListVo;
import com.cskaoyan.bean.wx.footprint.FootPrintVo;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.service.FootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: FootprintServiceImpl
 * Description:
 * date: 2022/6/5 20:32
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Override
    public FootprintData query(BaseParam param, Integer userId, Integer goodsId) {
        //开启分页
        PageHelper.startPage(param.getPage(),param.getLimit());
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        //添加规则
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        //排序规则
        example.setOrderByClause("add_time desc");
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketFootprints);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return FootprintData.data(total,pages,param.getLimit(),param.getPage(),marketFootprints);
    }
/**
 * @Description: 小程序历史足迹
  * @param
 * @return:
 * @Author: 蒋镇泽
 * @Date: 2022/6/8 15:28
 */
@Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public FootPrintVo wxQueryByLimit(BaseParamBo baseParamBo) {
        FootPrintVo footPrintVo = new FootPrintVo();
        footPrintVo.setLimit(baseParamBo.getLimit());
        footPrintVo.setPage(baseParamBo.getPage());
        //获得当前用户id
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = null;
        if (subject.isAuthenticated()) {
            primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        }
        Integer userId = primaryPrincipal.getId();
        //获得总数
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(userId);
        long i = marketFootprintMapper.countByExample(marketFootprintExample);
        int total = Math.toIntExact(i);
        footPrintVo.setTotal(Math.toIntExact(total));
        //获得总页数
        int pages= (int) Math.ceil((double)total/(double)baseParamBo.getLimit());
        footPrintVo.setPages(pages);
        //根据当前页数查出limit条数据
        MarketFootprintExample marketFootprintExample1 = new MarketFootprintExample();
        Integer page = baseParamBo.getPage();
        Integer limit = baseParamBo.getLimit();
        // int end=(page)*(limit);
        // int start=end-baseParamBo.getLimit()+1;
        MarketFootprintExample.Criteria criteria1 = marketFootprintExample1.createCriteria();
        // criteria1.andIdBetween(start, end);
        criteria1.andDeletedEqualTo(false);
        criteria1.andUserIdEqualTo(userId);
        marketFootprintExample1.setOrderByClause("id desc");
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample1);
        ArrayList<FootPrintOfListVo> footPrintOfListVos = new ArrayList<>();

int temp=1;

        for (MarketFootprint marketFootprint : marketFootprints) {
            if(temp>0&&temp<=10){
                MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
                FootPrintOfListVo footPrintOfListVo = new FootPrintOfListVo();
                footPrintOfListVo.setBrief(marketGoods.getBrief());
                footPrintOfListVo.setPicUrl(marketGoods.getPicUrl());
                Date date = new Date();
                footPrintOfListVo.setAddTime(date);
                footPrintOfListVo.setGoodsId(marketGoods.getId());
                footPrintOfListVo.setName(marketGoods.getName());
                footPrintOfListVo.setId(marketGoods.getId());

                footPrintOfListVo.setRetailPrice(marketGoods.getRetailPrice());
                footPrintOfListVos.add(footPrintOfListVo);
            }
            temp++;
        }
        footPrintVo.setList(footPrintOfListVos);

        return footPrintVo;
    }

    @Override
    public void wxFootprintDelete(Integer id) {
        //把deleted变为true
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.createCriteria().andGoodsIdEqualTo(id);
        MarketFootprintExample marketFootprintExample2 = new MarketFootprintExample();
        marketFootprintExample2.createCriteria().andGoodsIdEqualTo(id);
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample2);
        if(marketFootprints!=null){
            for (MarketFootprint marketFootprint : marketFootprints) {
                marketFootprint.setDeleted(true);
            }
        }


        marketFootprintMapper.updateByExampleSelective(marketFootprints.get(0),marketFootprintExample);

    }
}
